package com.aischool.service;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;

public class AuthService {
	
	@Inject
	SysUserAppService sysUserAppService;
	@Inject
	SysMenuService sysMenuService;
	@Inject
	SysApiService sysApiService;
	@Inject
	SysPermissionService sysPermissionService;
	@Inject
	SysUserRoleService sysUserRoleService;

	/**
	 * 判断应用权限
	 * @param user_id
	 * @param app_id
	 * @return
	 */
	public boolean checkAppPower(int user_id, int app_id) {
		List<Record> listRd = sysUserAppService.getUserApp(user_id);
		for(Record rd : listRd) {
			if (rd.getInt("app_id").equals(app_id)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断用户权限
	 * @param user_id
	 * @param surl
	 * @return
	 */
	public boolean checkUserPower(int user_id, int app_id, String surl) {
		
		//List<Record> listRole = sysUserRoleService.getUserRole(user_id, app_id);
		// 菜单权限
		if (surl.startsWith("/admin/")) {
			if (surl.startsWith("/admin/eduorg/")) {
				List<Record> listPower = sysPermissionService.getAppUserMenu(app_id, user_id);
				for(Record rdPower : listPower) {
					if (surl.contains(rdPower.getStr("menu_url"))) {
						return true;
					}
				}
			}else {
				return true;
			}
		}else if (surl.startsWith("/api/")) {
			return true;
		}else if (surl.startsWith("/front/")) {
			return true;
		}
		
		// 默认，没有权限 return false;
		return false;
	}
}
